/*
 * Created on Jun 22, 2005
 */
package org.vectrics.event;

import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.vectrics.common.util.DateSync;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.Filter;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.hibernate.Selector;
import org.vectrics.user.Person;

/**
 * @author MMoore
 */
public class EventService {
	private transient Logger log = Logger.getLogger(EventService.class);
	
	
	public EventType findEventType(String eventTypeCode) {
		EventType eventType = (EventType)ThreadProperties.getSession().load(EventType.class, eventTypeCode);
		return(eventType);
	}
	
	public void updateEvent(Event event) {
		if (ThreadProperties.getEventsOff())
			return;
		log.debug("updateEvent() called"
				+ "\r\n - Event ID:        " + event.getId()
				+ "\r\n - Event desc:      " + event.getDescription()
				+ "\r\n - Number of attch: " + event.getAttachments().size()
				);
		//event.setCreatedBy(HibernateSessionStore.getPerson());
		if (event.getEventTime() == null)
			event.setEventTime(DateSync.getCurrentTime());
		
		ThreadProperties.getSession().saveOrUpdate(event);
		log.debug("updateEvent() finished");
	}

	
	public List findUnprocessedEvents(Domain domain, int startIndex, int endIndex, List<Long> eventIds) {
		Session session = ThreadProperties.getSession();
		session.evict(Event.class);
		session.getSessionFactory().evictQueries();
		
		if (log.isDebugEnabled())
			log.debug("Session cache mode: " + session.getCacheMode().toString());
		
		Filter filter = new Filter();
		filter.setConjunction(Selector.CONJUNTION_AND);
		filter.addFrom("Event", "event");
		filter.addSelectField("event");
		filter.addParameterCondition("event.status", "=", Event.STATUS_UNPROCESSED);
		filter.addParameterCondition("event.domain.id", "=", domain.getId());
		filter.addOrderBy("event.id");
		if ((eventIds != null) && (eventIds.size() > 0)) {
			Selector specificIdSelector = filter.getSelector().addChildSelector(Selector.CONJUNTION_OR);
			Iterator<Long> iter = eventIds.iterator();
			while (iter.hasNext()) {
				Long eventId = iter.next();
				specificIdSelector.addParameterCondition("event.id", "=", eventId);
			}
		}
		
		if (log.isDebugEnabled())
			log.debug("findUnprocessedEvents() - statement = \r\n" + filter.getStatement());
		
		Query query = null;
		query = filter.createQuery(session);
		if ((endIndex >= 0) && (startIndex >= 0)) {
			query.setMaxResults(1 + endIndex - startIndex);
			query.setFirstResult(startIndex);
		}
		query.setCacheable(false);

		return query.list();
	}

	public Event findEvent(Long eventId) {
		Session session = ThreadProperties.getSession();
		Event event = (Event)session.load(Event.class, eventId);
		return(event);
	}

	public List findNotifyUserGroups(EventType eventType) {
		Session session = ThreadProperties.getSession();
		session.evict(Person.class);
		Filter filter = new Filter();
		filter.setConjunction(Selector.CONJUNTION_OR);
		filter.addFrom("EventNotifyUserGroup", "eventNotifyUserGroup");
		filter.addSelectField("eventNotifyUserGroup");
		filter.addParameterCondition("eventNotifyUserGroup.eventType.code", "=", eventType.getCode());
		Query query = null;
		query = filter.createQuery(session);

		return query.list();
		
	}
	
}
